﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication5.Controller;
using WebApplication5.Model;

namespace WebApplication5.Pages
{
    public partial class projekt : System.Web.UI.Page
    {
        protected projectCtr pCtr = new projectCtr();
        protected personCtr perCtr = new personCtr();
        protected void Page_Load(object sender, EventArgs e)
        {
            Site1 s = Page.Master as Site1;
            s.SelectedMenu = "project";

            if (!IsPostBack)
            {
                refresh();
            }


            List<WebApplication5.Model.Project> pa = pCtr.GetAllProject();
            var datasource = from p in pa
                             select new
                             {
                                 p.name,
                                 p.status,
                                 p.cost,
                                 p.endDate,
                                 p.id,
                                 p.Login,
                                 p.loginId,
                                 p.startDate,
                                 p.time,
                                 p.Userstory,
                                 DisplayField = String.Format("{0} ({1})", p.name, p.status)
                             };
                
                //DropDownListEdit.DataSource = datasource;
                //DropDownListEdit.DataValueField = "id";
                //DropDownListEdit.DataTextField = "DisplayField";
                //DropDownListEdit.DataBind();


                //DropDownListDelete.DataSource = datasource;
                //DropDownListDelete.DataValueField = "id";
                //DropDownListDelete.DataTextField = "DisplayField";
                //DropDownListDelete.DataBind();

                //List<Model.Login> personP = perCtr.GetAllEmployee();
                //var datasourcePer = from pe in personP
                //                       select new
                //                       {
                //                           pe.companyId,
                //                           pe.firstName,
                //                           pe.id,
                //                           pe.lastName,
                //                           pe.password,
                //                           pe.phoneNumber,
                //                           pe.Project,
                //                           pe.userName,
                //                           pe.verified,
                //                           DisplayField = String.Format("{0} ({1})", pe.firstName, pe.lastName)
                //                       };
                //DropDownListEditPerson.DataSource = datasourcePer;
                //DropDownListEditPerson.DataValueField = "id";
                //DropDownListEditPerson.DataTextField = "DisplayField";
                //DropDownListEditPerson.DataBind();


                //List<Model.Login> perl = perCtr.GetAllEmployee();
                //var datasourcePerson = from pe in perl
                //                       select new
                //                       {
                //                           pe.companyId,
                //                           pe.firstName,
                //                           pe.id,
                //                           pe.lastName,
                //                           pe.password,
                //                           pe.phoneNumber,
                //                           pe.Project,
                //                           pe.userName,
                //                           pe.verified,
                //                           DisplayField = String.Format("{0} ({1})", pe.firstName, pe.lastName)
                //                       };
                //DropDownListAdd.DataSource = datasourcePerson;
                //DropDownListAdd.DataValueField = "id";
                //DropDownListAdd.DataTextField = "DisplayField";
                //DropDownListAdd.DataBind();
            
        }

        protected void refresh()
        {
            List<WebApplication5.Model.Project> ha = pCtr.GetAllProject();
            var datasource = from h in ha
                             select new
                             {
                                 h.id,
                                 h.name,
                                 h.status,
                                 h.cost,
                                 h.endDate,
                                 h.Login,
                                 h.loginId,
                                 h.startDate,
                                 h.time,
                                 h.Userstory,
                                 DisplayField = String.Format("{0} ({1})", h.name, h.status)
                             };

            DropDownListEdit.DataSource = datasource;
            DropDownListEdit.DataValueField = "id";
            DropDownListEdit.DataTextField = "DisplayField";
            DropDownListEdit.DataBind();


            DropDownListDelete.DataSource = datasource;
            DropDownListDelete.DataValueField = "id";
            DropDownListDelete.DataTextField = "DisplayField";
            DropDownListDelete.DataBind();


            List<Model.Login> perl = perCtr.GetAllEmployee();
            var datasourcePerson = from pe in perl
                                   select new
                                   {
                                       pe.companyId,
                                       pe.firstName,
                                       pe.id,
                                       pe.lastName,
                                       pe.password,
                                       pe.phoneNumber,
                                       pe.Project,
                                       pe.userName,
                                       pe.verfied,
                                       DisplayField = String.Format("{0} ({1})", pe.firstName, pe.lastName)
                                   };
            DropDownListAdd.DataSource = datasourcePerson;
            DropDownListAdd.DataValueField = "id";
            DropDownListAdd.DataTextField = "DisplayField";
            DropDownListAdd.DataBind();



                        List<Model.Login> perP = perCtr.GetAllEmployee();
            var datasourceP = from pe in perP
                                   select new
                                   {
                                       pe.companyId,
                                       pe.firstName,
                                       pe.id,
                                       pe.lastName,
                                       pe.password,
                                       pe.phoneNumber,
                                       pe.Project,
                                       pe.userName,
                                       pe.verfied,
                                       DisplayField = String.Format("{0} ({1})", pe.firstName, pe.lastName)
                                   };
            DropDownListEditPerson.DataSource = datasourceP;
            DropDownListEditPerson.DataValueField = "id";
            DropDownListEditPerson.DataTextField = "DisplayField";
            DropDownListEditPerson.DataBind();
        
        }


        protected void ButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                Project p = new Project();
                p.id = int.Parse(DropDownListEdit.SelectedValue);
                p.cost = double.Parse(priceTbe.Text);
                p.endDate = DateTime.Parse(endDateTbe.Text);
                p.Login = null;
                p.loginId = int.Parse(DropDownListEditPerson.SelectedValue);
                p.name = nameTbe.Text;
                p.startDate = DateTime.Parse(startDateTbe.Text);
                p.status = statusTbe.Text;
                p.time = int.Parse(timeTbe.Text);
                p.Userstory = null;
         

                pCtr.editProject(p);

                refresh();


            }


            catch (Exception exc)
            {
                string strm = exc.Message.Replace('\'', ' ');
                string str = "<script language='javascript'>";
                str += "alert('" + strm + "')";
                str += "</script>";
                ClientScript.RegisterStartupScript(typeof(Page), "showmessage", str);
            }

        }




        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            int id = int.Parse(DropDownListDelete.SelectedValue);

            pCtr.deleteProject(id);

            refresh();
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {

            try
            {
                Project pas = new Project();

                pas.cost = double.Parse(costTb.Text);
                pas.endDate = DateTime.Parse(endDateTb.Text);
                pas.loginId = int.Parse(DropDownListAdd.SelectedValue);
                pas.name = nameTb.Text;
                pas.startDate = DateTime.Parse(startDateTb.Text);
                pas.status = statusTb.Text;
                pas.time = int.Parse(timeTb.Text);
                pas.Userstory = null;


                pCtr.AddProject(pas);


                costTb.Text = "";
                endDateTb.Text = "";
                nameTb.Text = "";
                startDateTb.Text = "";
                statusTb.Text = "";
                timeTb.Text = "";

                refresh();
            }

            catch (Exception ex)
            {
                string strm = ex.Message.Replace('\'', ' ');
                string str = "<script language='javascript'>";
                str += "alert('" + strm + "')";
                str += "</script>";
                ClientScript.RegisterStartupScript(typeof(Page), "showmessage", str); 
            }


        }

        protected void ButtonGetProject_Click(object sender, EventArgs e)
        {


            int id = int.Parse(DropDownListEdit.SelectedValue);

            var H = pCtr.GetProject(id);

            
            priceTbe.Text = H.cost.ToString();
            endDateTbe.Text = H.endDate.ToString();
            nameTbe.Text = H.name;
            startDateTbe.Text = H.startDate.ToString();
            statusTbe.Text = H.status;
            timeTbe.Text = H.time.ToString();


        }
    
        
    }
}